# # 逻辑形
# v <- TRUE
# print(class(v))

# # 数字
# v <- 23.5
# print(class(v))

# # 整型
# v <- 2L
# print(class(v))

# # 复合型
# v <- 2+5i
# print(class(v))

# # 字符
# v <- "TRUE"
# print(class(v))

# # 原型
# v <- charToRaw("Hello")
# print(class(v))

# # 向量
# apple <- c('red', 'green', 'yellow')
# print(apple)
# print(class(apple))

# # 列表
# list1 <- list(c(2,5,3),21.3,sin)
# print(list1)

# # 矩阵
# M = matrix(c('a','a','b','c','b','a'), nrow=2,ncol=3,byrow=TRUE)
# print(M)

# # 数组
# a <- array(c('green','yellow'), dim=c(3,3,2))
# print(a)

# # 因子
# apple_colors <- c('green','green','yellow','red','red','red','green')
# factor_apple <- factor(apple_colors)
# print(factor_apple)
# print(nlevels(factor_apple))

# # 数据帧
# BMI <- 	data.frame(
#    gender = c("Male", "Male","Female"), 
#    height = c(152, 171.5, 165), 
#    weight = c(81,93, 78),
#    Age = c(42,38,26)
# )
# print(BMI)

# var.1 = c(0,1,2,3)
# var.2 <- c("learn", "R")
# c(TRUE,1) -> var.3

# print(var.1)
# cat("var.1 is ", var.1,"
# ")
# cat("var.2 is ", var.2,"
# ")
# cat("var.3 is ", var.3,"
# ")

# print(ls())


# # 向量相加
# v = c(2, 5.5, 6)
# t = c(8, 3, 4)
# print(v + t)

# # 向量相减
# print(v - t)

# # 向量相乘
# print(v * t)

# # 向量相除
# print(v / t)

# # 向量求余
# print(v %% t)

# # 向量相除求商
# print(v %/% t)

# # 将第二向量作为第一向量的指数
# print(v ^ t)


# # 检查第一个向量的每个元素是否大于第二个向量的相应元素
# v = c(2, 5.5, 6, 9)
# t = c(8, 2.5, 14, 9)
# print(v > t)

# # 检查第一个向量的每个元素是否小于第二个向量的相应元素
# print(v < t)

# # 检查第一个向量的每个元素是否等于第二个向量的相应元素
# print(v == t)

# # 检查第一个向量的每个元素是否小于或等于第二向量的相应元素
# print(v <= t)

# # 检查第一向量的每个元素是否大于或等于第二向量的相应元素
# print(v >= t)

# # 检查第一个向量的每个元素是否不等于第二个向量的相应的元素
# print(v != t)

# # 逻辑与运算符
# v = c(3, 1, TRUE, 2+3i)
# t = c(4, 1, FALSE, 2+3i)
# print(v & t)

# # 逻辑或运算符
# print(v | t)

# # 逻辑非运算符
# print(!v)

# # 冒号运算符： 它为向量按顺序创建一系列数字
# v = 2:8
# print(v)

# # 标识运算符是否属于向量
# v1 = 8
# v2 = 12
# t = 1:10
# print(v1 %in% t)
# print(v2 %in% t)

# # 将矩阵与其转置相乘
# M = matrix(c(2, 6, 5, 1, 10, 4), nrow=2,ncol=3,byrow=TRUE)
# t = M %*% t(M)
# print(t)

# # if语句
# x = 30L
# if(is.integer(x)) {
# 	print("X is an Integer")
# }

# # if...else...
# x = c("what", "is", "truth")
# if("Truth" %in% x) {
# 	print("Truth is found")
# } else {
# 	print("Truth is not found")
# }

# # switch语句
# x = switch(
# 		3,
# 		"first",
# 		"second",
# 		"third",
# 		"fourth"
# 	)
# print(x)

# # 获取R包路径
# .libPaths()

# # 获取所有软件包列表
# library()

# # 获取当前R环境中加载的所有包
# search()

# # 从CRAN网页获取软件包
# install.packages("spe", repos="https://cran.cnr.berkeley.edu/")

# # 手动安装
# install.packages("C:\\Users\\mazaiting\\Desktop\\spec_0.1.3.zip", repos = NULL, type = "source")

# # repeat语句
# v = c("Hello","loop")
# cnt = 2

# repeat {
#    print(v)
#    cnt = cnt+1
   
#    if(cnt > 5) {
#       break
#    }
# }

# # while循环语句
# v = c("Hello","while loop")
# cnt = 2

# while (cnt < 7) {
#    print(v)
#    cnt = cnt + 1
# }

# # for循环语句
# v <- LETTERS[1:4]
# for ( i in v) {
#    print(i)
# }

# # break语句
# v <- c("Hello","loop")
# cnt <- 2

# repeat {
#    print(v)
#    cnt <- cnt + 1
	
#    if(cnt > 5) {
#       break
#    }
# }

# # next语句
# v <- LETTERS[1:6]
# for ( i in v) {
   
#    if (i == "D") {
#       next
#    }
#    print(i)
# }


# # 数据帧加入列和行
# # 创建向量对象
# city = c("Tampa", "Seattle", "Hartford", "Denver")
# state = c("FL", "WA", "CT", "CO")
# zipcode = c(33602, 98104, 06161, 80294)
# # 将三个向量联合为一个数据帧
# addresses = cbind(city, state, zipcode)
# # 打印第一个输出提示
# cat("# # # # The first data frame 
# ")
# # 打印第一个数据帧
# print(addresses)
# # 创建另一个数据帧
# new.address = data.frame(
# 	city = c("Lowry", "Charlotte"),
# 	state = c("CO", "FL"),
# 	zipcode = c("80230", "33949"),
# 	stringsAsFactors = FALSE
# )
# # 打印第二个输出提示
# cat("# # # The Second data frame
# ")
# # 打印数据帧
# print(new.address)
# # 从数据帧中合并行
# all.addresses = rbind(addresses, new.address)
# # 打印第三个提示
# cat("# # # The combined data frame
# ")
# # 打印结果
# print(all.addresses)

# # 合并数据帧
# library(MASS)
# merged.Pima = merge(
# 	x = Pima.te, 
# 	y = Pima.tr,
# 	by.x = c("bp", "bmi"),
# 	by.y = c("bp", "bmi")
# 	# by.x = c("skin", "glu"),
# 	# by.y = c("skin", "glu")
# )
# # 打印合并后的数据
# print(merged.Pima)
# # 打印行数
# nrow(merged.Pima)

# library(MASS)
# # 打印船舶数据集
# print(ships)

# 此包中提供melt()和cast(), 需要下载
# install.packages("reshape2", repos = "https://cran.cnr.berkeley.edu/")
# melt()拆分数据
# library(reshape2)
# library(MASS)

# molten.ships = melt(ships, id = c("type", "year"))
# # print(molten.ships)

# # cast() 重构数据
# recasted.ship = dcast(molten.ships, type+year~variable,sum)
# print(recasted.ship)

# # 内置函数
# # 创建一串数字，从32到44
# print(seq(32, 44))

# # 中间数
# print(mean(25:82))

# # 从41加到68
# print(sum(41:68))

# # 用户定义的函数
# new.function <- function(a) {
# 	for (i in 1:a) {
# 		b <- i^2
# 		print(b)
# 	}
# }

# # 调用函数
# new.function(6)

# # 连接字符串 - paste()函数
# a <- "Hello"
# b <- 'How'
# c <- "are you? "

# print(paste(a,b,c))
# print(paste(a,b,c, sep = "-"))
# print(paste(a,b,c, sep = "", collapse = ""))

# # 格式化函数 - format()函数
# # 显示总位数
# result <- format(23.123456789, digits = 9)
# print(result)
# # 科学计数法显示
# result <- format(c(6, 13.14521), scientific = TRUE)
# print(result)
# # 小数右边最小位数
# result <- format(23.47, nsmall = 5)
# print(result)
# # 格式化为一个字符串
# result <- format(6)
# print(result)
# # 设置数据宽度
# result <- format(13.7, width = 6)
# print(result)
# # 设置对齐方式-左对齐
# result <- format("Hello", width = 8, justify = "l")
# print(result)
# # 设置对齐方式-居中
# result <- format("Hello", width = 8, justify = "c")
# print(result)

# # 计算字符数-nchar()
# result <- nchar("Count the number of characters.")
# print(result)

# # 更改大小写
# # 转换大写
# result <- toupper("Changin To Upper")
# print(result)
# # 转换小写
# result <- tolower("Changin To Upper")
# print(result)

# # 截取字符串
# result <- substring("Extract", 5, 7)
# print(result)

# # 创建向量
# # 字符向量
# print("abc")
# # 双精度向量
# print(12.5)
# # 整型向量
# print(63L)
# # 逻辑型向量
# print(TRUE)
# # 复数向量
# print(2+3i)
# # 原型向量
# print(charToRaw('Hello'))

# # 多元素向量
# # 创建序列5-13
# v <- 5:13
# print(v)
# # 创建序列6.6-12.6
# v <- 6.6:12.6
# print(v)
# # 如果最后的结点是特殊的，未在序列中定义
# v <- 3.8:11.4
# print(v)

# # 创建从5-9，涨幅为0.4的向量
# print(seq(5, 9, by = 0.4))

# # 使用c函数--如果其中一个元素是字符，则非字符值被强制转换为字符类型
# s <- c('apple', 'red', 5, TRUE)
# print(s)

# # 访问向量元素
# # 使用索引访问向量的元素。 []括号用于建立索引。 索引从位置1开始。在索引中给出负值会丢弃来自result.TRUE，FALSE或0和1的元素，也可用于索引。
# t <- c("Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat")
# # 使用坐标
# v <- t[c(2, 3, 6)]
# print(v)
# # 使用逻辑值
# v <- t[c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)]
# print(v)
# # 使用负数
# v <- t[c(-2,-5)]
# print(v)
# # 使用0/1
# v <- t[c(0,0,0,0,0,0,1)]
# print(v)

# # 向量操作
# # 创建两个向量
# v1 <- c(3, 8, 4, 5, 0, 11)
# v2 <- c(4, 11, 0, 8, 1, 2)

# # 向量加法
# add.result <- v1 + v2
# print(add.result)

# # 向量减法
# sub.result <- v1 - v2
# print(sub.result)

# # 向量乘法
# multi.result <- v1 * v2
# print(multi.result)

# # 向量除法
# div.result <- v1 / v2 
# print(div.result)

# # 向量元素回收
# # 如果我们对不等长的两个向量应用算术运算，则较短向量的元素被循环使用
# v1 <- c(3, 8, 4, 5, 0, 11)
# v2 <- c(4, 11)
# # v2 -> c(4, 11, 4, 11, 4, 11)

# add.result <- v1 + v2
# print(add.result)

# sub.result <- v1 - v2
# print(sub.result)

# # 向量排序
# v <- c(3, 8, 4, 5, 0, 11, -9, 304)

# # 排序
# sort.result <- sort(v)
# print(sort.result)

# # 递减排序
# revsort.result <- sort(v, decreasing = TRUE)
# print(revsort.result)

# # 字符排序
# v <- c("Red", "Blue", "yellow", "violet")
# sort.result <- sort(v)
# print(sort.result)

# # 递减排序
# revsort.result <- sort(v, decreasing = TRUE)
# print(revsort.result)

# # 列表
# # 创建列表
# list_data <- list("Red", "Green", c(21, 32, 11), TRUE, 51.23, 119.1)
# print(list_data)

# # 命名列表元素
# # 创建一个包含向量，矩阵，列表的列表
# list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))
# # 命名
# names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
# print(list_data)

# # 访问列表元素
# # 创建一个包含向量，矩阵，列表的列表
# list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))
# # 命名
# names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
# # 打印第一个结点数据
# print(list_data[1])
# print("===========================")
# # 打印第三个结点数据
# print(list_data[3])
# print("===========================")
# # 使用名字访问
# print(list_data$A_Matrix)
# print("===========================")

# # 操控列表元素
# # 创建一个包含向量，矩阵，列表的列表
# list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))
# # 命名
# names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
# # 在列表的末尾添加节点
# list_data[4] <- "New element"
# print(list_data[4])
# # 移除最后一个节点
# list_data[4] <- NULL
# print(list_data[4])
# # 更新第三个节点
# list_data[3] <- "updated element"
# print(list_data[3])

# # 合并列表
# list1 <- list(1, 2, 3)
# list2 <- list("Sun", "Mon", "Tue")
# # 合并两个列表
# merged.list <- c(list1, list2)
# print(merged.list)

# # 列表转换为向量--unlist()函数
# list1 <- list(1:5)
# print(list1)
# list2 <- list(10:14)
# print(list2)
# # 将列表转换为向量
# v1 <- unlist(list1)
# v2 <- unlist(list2)

# print(v1)
# print(v2)
# # 计算和
# result <- v1 + v2
# print(result)

# # 矩阵
# # 创建矩阵
# # 按行排列
# M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
# print(M)
# # 按列排列
# M <- matrix(c(3:14), nrow = 4, byrow = FALSE)
# print(M)
# # 定义行和列的名字
# rowName = c("row1", "row2", "row3", "row4")
# colName = c("col1", "col2", "col3")
# P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rowName, colName))
# print(P)

# # 访问矩阵元素
# rowName = c("row1", "row2", "row3", "row4")
# colName = c("col1", "col2", "col3")
# P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rowName, colName))

# # 打印第一行第三列
# print(P[1, 3])
# # 打印第四行第二列
# print(P[4, 2])
# # 仅打印第二行
# print(P[2, ])
# # 打印第三列
# print(P[ , 3])

# # 矩阵加法和减法
# matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
# print(matrix1)
# matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
# print(matrix2)

# # 矩阵加法
# result <- matrix1 + matrix2
# cat("Result of addition","
# ")
# print(result)

# # 矩阵减法
# result <- matrix1 - matrix2
# cat("Result of subtraction","
# ")
# print(result)


# # 矩阵乘法和除法
# matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
# print(matrix1)
# matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
# print(matrix2)

# # 矩阵乘法
# result <- matrix1 * matrix2
# cat("Result of multiplicaion","
# ")
# print(result)

# # 矩阵除法
# result <- matrix1 / matrix2
# cat("Result of division","
# ")
# print(result)

# # 数组
# # 创建数组
# vector1 <- c(5, 9, 3)
# vector2 <- c(10, 11, 12, 13, 14, 15)

# # 将向量输入到数组中,c(3, 3, 2),它创建2个矩形矩阵，每个矩阵具有3行和3列。 数组只能存储数据类型。
# result <- array(c(vector1, vector2), dim = c(3, 3, 2))
# print(result)

# # 命名行和列
# vector1 <- c(5, 9, 3)
# vector2 <- c(10, 11, 12, 13, 14, 15)
# # 命名
# column.names <- c("COL1", "COL2", "COL3")
# row.names <- c("ROW1", "ROW2", "ROW3")
# matrix.names <- c("Matrix1", "Matrix2")

# result <- array(c(vector1, vector2), dim = c(3, 3, 2), dimnames = list(row.names, column.names, matrix.names))
# print(result)

# # 访问数组元素
# vector1 <- c(5, 9, 3)
# vector2 <- c(10, 11, 12, 13, 14, 15)
# # 命名
# column.names <- c("COL1", "COL2", "COL3")
# row.names <- c("ROW1", "ROW2", "ROW3")
# matrix.names <- c("Matrix1", "Matrix2")

# result <- array(c(vector1, vector2), dim = c(3, 3, 2), dimnames = list(row.names, column.names, matrix.names))

# # 打印第二个矩阵第三行
# print(result[3, , 2])
# # 打印第一个矩阵第一行第三个数据
# print(result[1, 3, 1])
# # 打印第二个矩阵
# print(result[, , 2])

# # 操作数组元素
# vector1 <- c(5, 9, 3)
# vector2 <- c(10, 11, 12, 13, 14, 15)

# array1 <- array(c(vector1, vector2), dim = c(3, 3, 2))
# print(array1)
# print("============================")
# vector3 <- c(9, 1, 0)
# vector4 <- c(6, 0, 11, 3, 14, 1, 2, 6, 9)
# array2 <- array(c(vector3, vector4), dim = c(3, 3, 2))
# print(array2)
# print("============================")
# matrix1 <- array1[, , 2]
# matrix2 <- array2[, , 2]
# result <- matrix1 + matrix2
# print(result)

# # 跨数组元素的计算
# vector1 <- c(5, 9, 3)
# vector2 <- c(10, 11, 12, 13, 14, 15)

# new.array <- array(c(vector1, vector2), dim = c(3, 3, 2))
# print(new.array)

# # 其中c(1)代表行相加，c(2)代表列相加，c(3)代表矩阵和
# result <- apply(new.array, c(1), sum)
# print(result)

# # 因子
# # 创建向量
# data <- c("East", "West", "East", "North", "North", "East", "West", "West", "West", "East", "North")
# print(data)
# # 打印是否为因子
# print(is.factor(data))

# # 创建因子
# factor_data <- factor(data)
# print(factor_data)
# # 打印是否为因子
# print(is.factor(factor_data))

# # 为数据帧创建向量
# height <- c(132, 151, 162, 139, 166, 147, 122)
# weight <- c(48, 49, 66, 53, 67, 52, 40)
# gender <- c("male", "male", "female", "female", "male", "female", "male")
# # 创建数据帧
# input_data <- data.frame(height, weight, gender)
# print(input_data)
# # 测试性别是否是因子
# print(is.factor(input_data$gender))
# # 打印性别列等级
# print(input_data$gender)

# # 更改级别顺序
# data <- c("East","West","East","North","North","East","West","West","West","East","North")
# # 创建因子
# factor_data <- factor(data)
# print(factor_data)

# # 应用因子函数，重新设置因子级别
# new_order_data <- factor(factor_data, levels = c("East", "West", "North"))
# print(new_order_data)

# # 生成因子级别
# v <- gl(3, 4, labels = c("Tampa", "Seattle", "Boston"))
# print(v)

# # 数据帧
# # 创建数据帧
emp.data <- data.frame(
	emp_id = c(1:5),
	emp_name = c("Rick", "Dan", "Michelle", "Ryan", "Gary"),
	salary = c(623.3, 515.2, 611.0, 729.0, 843.25),
	start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")),
	stringsAsFactors = FALSE
)
# print(emp.data)

# 获取数据帧结构--str()
# str(emp.data)

# 数据框中的数据摘要
# print(summary(emp.data))

# 从数据帧中提取数据
# result <- data.frame(emp.data$emp_name, emp.data$salary)
# print(result)

# 添加列
# emp.data$dept <- c("IT", "Operations", "IT", "HR", "Finance")
# v <- emp.data
# print(v)

# 添加行
# 创建第二个数据帧
# emp.newdata <- data.frame(
# 	emp_id = c(6:8),
# 	emp_name = c("Rasmi", "Pranab", "Tusar"),
# 	salary = c(578.0, 722.5, 632.8),
# 	start_date = as.Date(c("2013-05-21","2013-07-30","2014-06-17")),
# 	# dept = c("IT", "Operations", "Finance"),
# 	stringsAsFactors = FALSE
# )

# emp.finaldata <- rbind(emp.data, emp.newdata)
# print(emp.finaldata)

